﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BLL
{
    public class ShipBLL
    {
        private readonly DAL.ShipDAL shipdal = new DAL.ShipDAL();
        private readonly OrderBLL orderbll = new OrderBLL();
        /// <summary>
        /// 获取requireShipDate(Order In Work内的订单自下单之日起加28天，
        /// Order On Houd 则从付款后算起加28天，有试身项的订单再加14天，重要信息每dely一次增加1天)
        /// </summary>
        public DateTime getReqShipDdate(DateTime createDate,string orderid)
        {
            DateTime ret = new DateTime();
            DataTable dt=orderbll.getOrderByID(orderid);
            ret=createDate.AddDays(28);
            OrderItemBLL orderitembll = new OrderItemBLL();
            DataTable items=orderitembll.getItemByOrderID(orderid);
            foreach (DataRow dr in items.Rows)
            {
                if (bool.Parse(dr["FT"].ToString()))
                {
                    ret = ret.AddDays(14);
                    break;
                }
            }
            return ret;
        }

        /// <summary>
        /// 判断当前的订单项是否已经发货
        /// </summary>
        public bool finishShip(string orderitemid)
        {
            DataTable dt=shipdal.getShip("orderItemID="+orderitemid);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 选择订单项，出货（同时判断该订单项所属订单是否已经完全出货，如果是的话将订单改为 order complete）
        /// </summary>
        public void addShipMent(Albon.Model.ShipMent shipment)
        {
            shipdal.addShipMent(shipment);
            OrderItemBLL orderitembll = new OrderItemBLL();
            orderitembll.completeOrder(shipment.orderItemID);
        }

        /// <summary>
        /// 首先获取据有的订单项，然后查询ShipMent表，为已发货的项增加一个字段 ship=1
        /// </summary>
        /// <param name="idlist"></param>
        public DataTable getItemShipView(string idlist)
        {
            DAL.OrderItemDAL orderitemdal = new DAL.OrderItemDAL();
            DataTable dt=orderitemdal.getItemShipView(idlist);
            dt.Columns.Add("ship");
            foreach (DataRow dr in dt.Rows)
            {
                if (shipdal.getShip("orderItemID=" + dr["ID"].ToString()).Rows.Count == 0)
                {
                    dr["ship"] = false;
                }
                else
                {
                    dr["ship"] = true;
                }
            }
            return dt;
        }
        /// <summary>
        /// 获取订单中各项的运费信息（数量，试身，单重）
        /// </summary>
        public DataTable getShipChargeView(string orderid)
        {
            return shipdal.getShipChargeView(orderid);
        }
    }
}
